<?php
/**
 * (c) 2010 Philip Clarke, Bouncing Limited.
**/

if( SECURITY_READONLY_LOCK=='false' && SECURITY_READONLY_COUNTER==0){

  $fc->unlockRecursive(DIR_FS_CATALOG);
  $fc->unlock(dirname(DIR_FS_CATALOG));
  $fc->relock(DIR_FS_CATALOG.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->relock(DIR_FS_CATALOG.MODULE_AUTO_UPDATE_LOCATION.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);

  $sql = "UPDATE ".TABLE_CONFIGURATION." SET configuration_value='".time()."' WHERE configuration_key='SECURITY_READONLY_COUNTER'";

  $query = $db->execute($sql);

}else

if( ( SECURITY_READONLY_LOCK=='false' && (time() - SECURITY_READONLY_COUNTER) > 2*60*60 ) || (SECURITY_READONLY_LOCK == 'true' && SECURITY_READONLY_COUNTER !=0 ) )
{

  $fc->relockRecursive(DIR_FS_CATALOG);
  $fc->relock(dirname(DIR_FS_CATALOG));
  $fc->relock(DIR_FS_CATALOG.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->relock(DIR_FS_CATALOG.MODULE_AUTO_UPDATE_LOCATION.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->unlockRecursive(preg_replace('/\/$/', '', DIR_FS_SQL_CACHE));

  $sql = "UPDATE ".TABLE_CONFIGURATION." SET configuration_value='true' WHERE configuration_key='SECURITY_READONLY_LOCK'";

  $query = $db->execute($sql);

  $sql = "UPDATE ".TABLE_CONFIGURATION." SET configuration_value='0' WHERE configuration_key='SECURITY_READONLY_COUNTER'";

  $query = $db->execute($sql);


}


$sql = "SELECT UNIX_TIMESTAMP(last_modified) AS lrun FROM ".TABLE_CONFIGURATION." WHERE configuration_key='SECURITY_READONLY_LOCK'";

$query = $db->execute($sql);

if( (time()-$query->fields['lrun']) > 12*60*60 ){

  $sql = "UPDATE ".TABLE_CONFIGURATION." SET last_modified=NOW(), configuration_value='true' WHERE configuration_key='SECURITY_READONLY_LOCK'";

  $query = $db->execute($sql);

  $fc->relockRecursive(DIR_FS_CATALOG);
  $fc->relock(dirname(DIR_FS_CATALOG));
  $fc->relock(DIR_FS_CATALOG.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->relock(DIR_FS_CATALOG.MODULE_AUTO_UPDATE_LOCATION.'/'.DIR_WS_INCLUDES.'/configure.php', SECURITY_DIR_LOCKED, 0400);
  $fc->unlockRecursive(preg_replace('/\/$/', '', DIR_FS_SQL_CACHE));


}


